package com.lagou.edu.service.impl;

import com.lagou.edu.service.DemoService;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@Service
public class DemoServiceImpl implements DemoService {

    Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);

    @Override
    public String getDemoIp() {
        String ip = RpcContext.getContext().getAttachment(REMOTE_IP_CONTEXT_KEY);
        logger.info("接收到请求，IP为：{}", ip);
        return ip;
    }

    @Override
    public String methodA() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(100));
        return "success Request method A[server 2]";
    }

    @Override
    public String methodB() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(100));
        return "success Request method B[server 2]";
    }

    @Override
    public String methodC() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(100));
        return "success Request method C[server 2]";
    }
}
